"""测试配置文件

定义pytest测试夹具和配置
提供测试数据库、测试客户端等共享资源
"""

import pytest
import asyncio
from fastapi.testclient import TestClient
from datetime import datetime
import tempfile
import os

from main import app


@pytest.fixture(scope="session")
def event_loop():
    """创建事件循环"""
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()


@pytest.fixture(scope="session")
def client():
    """创建测试客户端"""
    with TestClient(app) as test_client:
        yield test_client


@pytest.fixture
def sample_session_data():
    """示例会话数据"""
    return {
        "id": f"test_session_{int(datetime.now().timestamp())}",
        "room_id": "test_room_001",
        "user_id": "test_user_001",
        "start_time": datetime.now().isoformat(),
        "end_time": None,
        "total_seconds": 0
    }


@pytest.fixture
def sample_eeg_data():
    """示例脑波数据"""
    # 生成模拟的脑波数据
    wave_frames = [float(i * 0.1) for i in range(100)]
    filtered_frames = [float(i * 0.05) for i in range(100)]
    
    return {
        "id": f"test_session_{int(datetime.now().timestamp())}",
        "eeg_wave_data": {
            "frame_rate": 250,
            "wave": {"frames": wave_frames},
            "filtered": {"frames": filtered_frames}
        },
        "delta_data": {
            "frame_rate": 250,
            "wave": {"frames": wave_frames[:50]},
            "filtered": {"frames": filtered_frames[:50]}
        },
        "theta_data": {
            "frame_rate": 250,
            "wave": {"frames": wave_frames[25:75]},
            "filtered": {"frames": filtered_frames[25:75]}
        },
        "low_alpha_data": {
            "frame_rate": 250,
            "wave": {"frames": wave_frames[10:60]},
            "filtered": {"frames": filtered_frames[10:60]}
        },
        "high_alpha_data": {
            "frame_rate": 250,
            "wave": {"frames": wave_frames[20:70]},
            "filtered": {"frames": filtered_frames[20:70]}
        },
        "low_beta_data": {
            "frame_rate": 250,
            "wave": {"frames": wave_frames[30:80]},
            "filtered": {"frames": filtered_frames[30:80]}
        },
        "high_beta_data": {
            "frame_rate": 250,
            "wave": {"frames": wave_frames[40:90]},
            "filtered": {"frames": filtered_frames[40:90]}
        },
        "gamma_data": {
            "frame_rate": 250,
            "wave": {"frames": wave_frames[50:100]},
            "filtered": {"frames": filtered_frames[50:100]}
        }
    }


@pytest.fixture
def sample_analysis_data():
    """示例分析数据"""
    return {
        "id": f"test_session_{int(datetime.now().timestamp())}",
        "analysis_type": "sleep_monitoring",
        "ratios": {
            "delta": 0.25,
            "theta": 0.20,
            "alpha": 0.30,
            "beta": 0.20,
            "gamma": 0.05
        },
        "states": ["awake", "light_sleep", "deep_sleep", "rem_sleep"],
        "confidence": [0.85, 0.92, 0.78, 0.88],
        "result": {
            "sleep_stage": "deep_sleep",
            "quality_score": 85.5,
            "duration_minutes": 120,
            "efficiency": 0.92
        },
        "created_at": datetime.now().isoformat()
    }